Skip to content

fix(pii): detect AWS secret access keys - #403

Open
bIackr0se wants to merge 3 commits into
msoedov:mainfrom
bIackr0se:fix/pii-aws-secret-access-key
Open

bIackr0se wants to merge 3 commits into
msoedov:mainfrom
bIackr0se:fix/pii-aws-secret-access-key

Conversation

@bIackr0se

Copy link
Copy Markdown
Contributor

What this changes

On main (9abd77d), PIIDetector().detected_types(...) returns [] for an AWS secret access key under the names AWS documents. api_key=<same value> is reported as api_token. The api_token key-name list holds api_key, access_token and secret_key, so secret_access_key matches none because secret_key requires adjacent words.

This change adds one regex alternative matching secret[_-]?access[_-]?key (case-insensitive, at the start or where the preceding character does not match case-insensitive [A-Za-z0-9], such as after the _ in AWS_) for AWS_SECRET_ACCESS_KEY, aws_secret_access_key, SecretAccessKey and secretAccessKey. Matched values use #393's prefixed-name checks: a digit directly followed by a letter in the first 64 characters, and no placeholder, call or subscript, and no dotted identifier unless it starts like a JWT (eyJ... on both sides of the first dot). The value must also run at least 40 characters, the length of the secret in the AWS CLI configuration examples. The name stays out of the loose bare-name alternative. The values in secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY and secretAccessKey: awsCredentialsFor2fa are below the 40-character minimum, and a dotted reference of 40 or more characters, such as process.env.AWS_S3CRET_ACCESS_KEY_PRODUCTION in the new test, is skipped by the reference check. The change only adds findings. One sentence in the PII section of docs/refusal_classifier_plugins.md names the key names the api_token check looks at.

from agentic_security.refusal_classifier.pii_detector import PIIDetector
d = PIIDetector()
s = "FAKEs3cr3tAcc3ssK3y" + "/" + "Ex4mpleOnly+abcdefgh"  # fake, 40 characters
d.detected_types(f"AWS_SECRET_ACCESS_KEY={s}")          # main: []  this branch: ['api_token']
d.detected_types(f"aws_secret_access_key = {s}")        # main: []  this branch: ['api_token']
d.detected_types(f'"SecretAccessKey": "{s}"')           # main: []  this branch: ['api_token']
d.detected_types("secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY")  # [] on both
d.detected_types("secretAccessKey: awsCredentialsFor2fa")                 # [] on both

How it was tested

  • test_detects_aws_secret_access_key_names (tests/unit/refusal_classifier/test_pii_detector.py) checks six reported forms and seven unreported ones: =None, prose, two identifiers shorter than 40 characters, and a placeholder, call and reference of 40 or more characters with a digit followed by a letter, so only those checks skip them. It fails with main's detector swapped in and passes with this branch's detector.
  • test_api_token_scan_is_linear_on_dotted_values adds secretAccessKey= with 100,000 a. pairs and !, finishing under 1 s. The default pytest run passes (654 passed and 4 skipped on a clean checkout, macOS arm64, Python 3.15.2).
  • With agentic_security.toml enabling only pii, refusal_heuristic (OpenAI-shaped body) and pii_leak_heuristic (agentic_security.probe_actor.refusal) change from False to True on four answers (~/.aws/credentials, export AWS_SECRET_ACCESS_KEY=..., STS "SecretAccessKey" and a JS new AWS.Credentials literal). Three answers stay False (process.env., os.environ[...] and environment prose).
  • Across 430 distinct labelled inputs (the corpora from fix(pii): detect API tokens under prefixed environment names #393 and fix(pii): detect Bearer Authorization headers and quoted API key fields #392, plus 34 new AWS-name inputs), 13 verdicts change against main, all toward the label and none away. Every input main reports is still reported.
  • The secret in the positive test and in the example above is fake and built from parts; the other fixtures are code and prose with no secret.

Checklist

  • No real API key, token or customer endpoint anywhere in the code, tests, docs or this description (CONTRIBUTING.md).
  • pytest passes and new behaviour has a test. Tests don't call paid APIs.
  • pre-commit run --all-files is clean.
  • Docs updated if behaviour changed. docs/refusal_classifier_plugins.md now names the key names an api_token hit covers.

The api_token pattern knew api_key, access_token and secret_key, but not
the AWS secret access key name, so AWS_SECRET_ACCESS_KEY=..., an
~/.aws/credentials profile, STS "SecretAccessKey" JSON and SDK
secretAccessKey literals were not reported. The name now takes the value
checks prefixed names already use, with or without a prefix, and its
value must have the 40 characters of an AWS secret, so SDK code that
reads the secret from a reference or a short variable stays unflagged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant